{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# all_interval"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/all_interval.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/all_interval.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  All interval problem in Google CP Solver.\n",
    "\n",
    "  CSPLib problem number 7\n",
    "  http://www.cs.st-andrews.ac.uk/~ianm/CSPLib/prob/prob007/index.html\n",
    "  '''\n",
    "  Given the twelve standard pitch-classes (c, c , d, ...), represented by\n",
    "  numbers 0,1,...,11, find a series in which each pitch-class occurs exactly\n",
    "  once and in which the musical intervals between neighbouring notes cover\n",
    "  the full set of intervals from the minor second (1 semitone) to the major\n",
    "  seventh (11 semitones). That is, for each of the intervals, there is a\n",
    "  pair of neigbhouring pitch-classes in the series, between which this\n",
    "  interval appears. The problem of finding such a series can be easily\n",
    "  formulated as an instance of a more general arithmetic problem on Z_n,\n",
    "  the set of integer residues modulo n. Given n in N, find a vector\n",
    "  s = (s_1, ..., s_n), such that (i) s is a permutation of\n",
    "  Z_n = {0,1,...,n-1}; and (ii) the interval vector\n",
    "  v = (|s_2-s_1|, |s_3-s_2|, ... |s_n-s_{n-1}|) is a permutation of\n",
    "  Z_n-{0} = {1,2,...,n-1}. A vector v satisfying these conditions is\n",
    "  called an all-interval series of size n; the problem of finding such\n",
    "  a series is the all-interval series problem of size n. We may also be\n",
    "  interested in finding all possible series of a given size.\n",
    "  '''\n",
    "\n",
    " Compare with the following models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/all_interval.mzn\n",
    "  * Comet   : http://www.hakank.org/comet/all_interval.co\n",
    "  * Gecode/R: http://www.hakank.org/gecode_r/all_interval.rb\n",
    "  * ECLiPSe : http://www.hakank.org/eclipse/all_interval.ecl\n",
    "  * SICStus : http://www.hakank.org/sicstus/all_interval.pl\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def main(n=12):\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"All interval\")\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  print(\"n:\", n)\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  x = [solver.IntVar(1, n, \"x[%i]\" % i) for i in range(n)]\n",
    "  diffs = [solver.IntVar(1, n - 1, \"diffs[%i]\" % i) for i in range(n - 1)]\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  solver.Add(solver.AllDifferent(x))\n",
    "  solver.Add(solver.AllDifferent(diffs))\n",
    "\n",
    "  for k in range(n - 1):\n",
    "    solver.Add(diffs[k] == abs(x[k + 1] - x[k]))\n",
    "\n",
    "  # symmetry breaking\n",
    "  solver.Add(x[0] < x[n - 1])\n",
    "  solver.Add(diffs[0] < diffs[1])\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  solution = solver.Assignment()\n",
    "  solution.Add(x)\n",
    "  solution.Add(diffs)\n",
    "\n",
    "  db = solver.Phase(x, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    print(\"x:\", [x[i].Value() for i in range(n)])\n",
    "    print(\"diffs:\", [diffs[i].Value() for i in range(n - 1)])\n",
    "    num_solutions += 1\n",
    "    print()\n",
    "\n",
    "  print(\"num_solutions:\", num_solutions)\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "\n",
    "\n",
    "n = 12\n",
    "if len(sys.argv) > 1:\n",
    "  n = int(sys.argv[1])\n",
    "main(n)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
